﻿// 功能：Native API调用
// 描述：
// 作者：温伟鹏
// 日期：2010/5/26 15:12:54

using System;
using System.Runtime.InteropServices;
using System.Diagnostics.CodeAnalysis;
using System.Drawing;

namespace MeizuSDK.Drawing
{
    /// <summary>
    /// Native API调用
    /// </summary>
    public class UnsafeNativeMethods
    {
        // Fields
        public const int BILINEAR = 5;
        public const int BITMAP_SIZE = 0x18;
        public const int BLACKONWHITE = 1;
        public const int COLORONCOLOR = 3;
        public const int DI_NORMAL = 3;
        public const int GN_CONTEXTMENU = 0x3e8;
        public const int GWL_STYLE = -16;
        public const int NULL_BRUSH = 5;
        public const int NULL_PEN = 8;
        public const int OBJ_BITMAP = 7;
        public const int OBJ_BRUSH = 2;
        public const int OBJ_DC = 3;
        public const int OBJ_ENHMETADC = 12;
        public const int OBJ_ENHMETAFILE = 13;
        public const int OBJ_FONT = 6;
        public const int OBJ_MEMDC = 10;
        public const int OBJ_PAL = 5;
        public const int OBJ_PEN = 1;
        public const int OBJ_REGION = 8;
        public const uint PM_NOREMOVE = 0;
        public const uint PM_REMOVE = 1;
        public const int PS_SOLID = 0;
        public const uint SHDB_HIDE = 2;
        public const uint SHDB_SHOW = 1;
        public const int SHRG_RETURNCMD = 1;
        public const int SRCCOPY = 0xcc0020;
        public const uint WM_MOUSEFIRST = 0x200;
        public const uint WM_MOUSELAST = 0x20e;
        public const uint WS_NONAVDONEBUTTON = 0x10000;

        // Methods
        [DllImport("coredll.dll")]
        public static extern int AlphaBlend(IntPtr hdcDest, int xDest, int yDest, int cxDest, int cyDest, IntPtr hdcSrc, int xSrc, int ySrc, int cxSrc, int cySrc, BlendFunction blendFunction);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern IntPtr CreateCompatibleDC(IntPtr hdc);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern IntPtr CreatePen(int type, int width, int color);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern IntPtr CreateSolidBrush(int color);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int DeleteDC(IntPtr hdc);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int DeleteObject(IntPtr hdc);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int DrawIconEx(IntPtr hdc, int xLeft, int yTop, IntPtr hIcon, int cxWidth, int cyWidth, int istepIfAniCur, IntPtr hbrFlickerFreeDraw, int diFlags);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int Ellipse(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int FillRect(IntPtr hdc, ref Rectangle lpRect, IntPtr hbrush);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern IntPtr GetDC(IntPtr window);
        [DllImport("coredll.dll", SetLastError = true)]
        internal static extern int GetObject(IntPtr hgdiobj, int cbBuffer, ref BITMAP lpvObject);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int GetObjectType(IntPtr hgdiobj);

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern IntPtr GetStockObject(int fnObject);
        [DllImport("coredll.dll")]
        public static extern uint GetWindowLong(IntPtr hWnd, int nIndex);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int IntersectClipRect(IntPtr window, int left, int top, int right, int bottom);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("coredll.dll")]
        public static extern bool PeekMessage(out NativeMessage message, IntPtr windowPointer, uint messageFilterMin, uint messageFilterMax, uint remove);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int Polygon(IntPtr hdc, Point[] points, int count);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int Polyline(IntPtr hdc, Point[] points, int count);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int ReleaseDC(IntPtr window, IntPtr hdc);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int RoundRect(IntPtr hdc, int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidth, int nHeight);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int SelectClipRgn(IntPtr hdc, IntPtr hregion);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);
        [DllImport("coredll.dll")]
        public static extern uint SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int StretchBlt(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, int dwRop);
        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int TransparentImage(IntPtr hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, IntPtr hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, int crTransparent);

        // Nested Types
        public enum AlphaFormat : byte
        {
            AC_SRC_ALPHA = 1
        }

        [StructLayout(LayoutKind.Sequential)]
        internal struct BITMAP
        {
            public int bmType;
            public int bmWidth;
            public int bmHeight;
            public int bmWidthBytes;
            public short bmPlanes;
            public short bmBitsPixel;
            [SuppressMessage("Microsoft.Security", "CA2111:PointersShouldNotBeVisible")]
            public IntPtr bmBits;
        }

        public enum BlendFlags : byte
        {
            Zero = 0
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct BlendFunction
        {
            public byte BlendOp;
            public byte BlendFlags;
            public byte SourceConstantAlpha;
            public byte AlphaFormat;
        }

        public enum BlendOperation : byte
        {
            AC_SRC_OVER = 0
        }

        public enum BufferDisposalFlag
        {
            BufferDisposalFlagNone,
            BufferDisposalFlagGlobalFree,
            BufferDisposalFlagCoTaskMemFree,
            BufferDisposalFlagUnmapView
        }

        [ComImport, Guid("327ABDA9-072B-11D3-9D7B-0000F81EF32E"), ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IImage
        {
            uint GetPhysicalDimension(out Size size);
            uint GetImageInfo(out ImageInfo info);
            uint SetImageFlags(uint flags);
            uint Draw(IntPtr hdc, ref Rectangle dstRect, IntPtr NULL);
            uint PushIntoSink();
            uint GetThumbnail(uint thumbWidth, uint thumbHeight, out UnsafeNativeMethods.IImage thumbImage);
        }

        public enum InterpolationHint
        {
            InterpolationHintDefault,
            InterpolationHintNearestNeighbor,
            InterpolationHintBilinear,
            InterpolationHintAveraging,
            InterpolationHintBicubic
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct NativeMessage
        {
            private IntPtr WindowPoint;
            public uint Message;
            private IntPtr HighWordParam;
            private IntPtr LowWordParam;
            public int Time;
            public int PointX;
            public int PointY;
        }

        public enum PixelFormatID
        {
            PixelFormat16bppARGB1555 = 0x61007,
            PixelFormat16bppRGB555 = 0x21005,
            PixelFormat16bppRGB565 = 0x21006,
            PixelFormat1bppIndexed = 0x30101,
            PixelFormat24bppRGB = 0x21808,
            PixelFormat32bppARGB = 0x26200a,
            PixelFormat32bppPARGB = 0xe200b,
            PixelFormat32bppRGB = 0x22009,
            PixelFormat48bppRGB = 0x10300c,
            PixelFormat4bppIndexed = 0x30402,
            PixelFormat64bppARGB = 0x34400d,
            PixelFormat64bppPARGB = 0x1c400e,
            PixelFormat8bppIndexed = 0x30803,
            PixelFormatAlpha = 0x40000,
            PixelFormatCanonical = 0x200000,
            PixelFormatDontCare = 0,
            PixelFormatExtended = 0x100000,
            PixelFormatGDI = 0x20000,
            PixelFormatIndexed = 0x10000,
            PixelFormatMax = 15,
            PixelFormatPAlpha = 0x80000,
            PixelFormatUndefined = 0
        }

        private enum SMS_ADDRESS_TYPE
        {
            SMSAT_UNKNOWN,
            SMSAT_INTERNATIONAL,
            SMSAT_NATIONAL,
            SMSAT_NETWORKSPECIFIC,
            SMSAT_SUBSCRIBER,
            SMSAT_ALPHANUMERIC,
            SMSAT_ABBREVIATED,
            SMS_ADDRESS_TYPE
        }

        public enum SourceConstantAlpha : byte
        {
            Opaque = 0xff,
            Transparent = 0
        }

    }
}
